clear
set mem 80m
set more off
set matsize 800
capture log close

cd "D:\MyPapers\Happiness\TWINSDATA\code\2023May-Dataverse"
use twins_PNAS.dta, clear
keep if mz

gen Age_1825 = age<=2.5 if age~=.
gen Age_2635 = age>2.5 & age<=3.5 if age~=.
gen Age_3645 = age>3.5 & age<=4.5 if age~=.
gen Age_4655 = age>4.5 & age<=5.5 if age~=.
gen Age_above55 = age>5.5 if age~=.

gen Int_Inc_Age2635 = income*Age_2635
gen Int_Inc_Age3645 = income*Age_3645
gen Int_Inc_Age4655 = income*Age_4655
gen Int_Inc_Ageabove55 = income*Age_above55

gen IV_Int_Inc_Age2635 = IVincome*Age_2635
gen IV_Int_Inc_Age3645 = IVincome*Age_3645
gen IV_Int_Inc_Age4655 = IVincome*Age_4655
gen IV_Int_Inc_Ageabove55 = IVincome*Age_above55

//use first difference
keep         Dhappiness Dhappy_4s Dincome IVDincome DBirthOrder_ElderDummy Dbirth_weight DVeryEarly_disease_d Dearly_disease_d Dedu Dmarried Ddivorced Dwidowed Dpopulation Dworking Dwork_over_40h male  ///
        Age_2635 Age_3645 Age_4655 Age_above55 Int_Inc_Age2635 Int_Inc_Age3645 Int_Inc_Age4655 Int_Inc_Ageabove55 IV_Int_Inc_Age2635 IV_Int_Inc_Age3645 IV_Int_Inc_Age4655 IV_Int_Inc_Ageabove55 twinpair twcode ///
			 
reshape wide Dhappiness Dhappy_4s Dincome IVDincome DBirthOrder_ElderDummy Dbirth_weight DVeryEarly_disease_d Dearly_disease_d Dedu Dmarried Ddivorced Dwidowed Dpopulation Dworking Dwork_over_40h male  /// 
        Age_2635 Age_3645 Age_4655 Age_above55 Int_Inc_Age2635 Int_Inc_Age3645 Int_Inc_Age4655 Int_Inc_Ageabove55 IV_Int_Inc_Age2635 IV_Int_Inc_Age3645 IV_Int_Inc_Age4655 IV_Int_Inc_Ageabove55, i(twinpair) j(twcode)
			  
gen DInt_Inc_Age2635 = Int_Inc_Age26351-Int_Inc_Age26352
gen DInt_Inc_Age3645 = Int_Inc_Age36451-Int_Inc_Age36452
gen DInt_Inc_Age4655 = Int_Inc_Age46551-Int_Inc_Age46552
gen DInt_Inc_Ageabove55 = Int_Inc_Ageabove551-Int_Inc_Ageabove552

gen IVDInt_Inc_Age2635 = IV_Int_Inc_Age26351-IV_Int_Inc_Age26352
gen IVDInt_Inc_Age3645 = IV_Int_Inc_Age36451-IV_Int_Inc_Age36452
gen IVDInt_Inc_Age4655 = IV_Int_Inc_Age46551-IV_Int_Inc_Age46552
gen IVDInt_Inc_Ageabove55 = IV_Int_Inc_Ageabove551-IV_Int_Inc_Ageabove552

reshape long

collapse      Dhappiness Dhappy_4s Dincome IVDincome DBirthOrder_ElderDummy Dbirth_weight DVeryEarly_disease_d Dearly_disease_d Dedu Dmarried Ddivorced Dwidowed Dpopulation Dworking Dwork_over_40h male ///
       Age_2635 Age_3645 Age_4655 Age_above55 DInt_Inc_Age2635 DInt_Inc_Age3645 DInt_Inc_Age4655 DInt_Inc_Ageabove55 IVDInt_Inc_Age2635 IVDInt_Inc_Age3645 IVDInt_Inc_Age4655 IVDInt_Inc_Ageabove55, by(twinpair)

	  
/*IVFE*/
ivreg2 Dhappy_4s (Dincome =IVDincome) DBirthOrder_ElderDummy Dbirth_weight Dearly_disease_d Dedu Dworking Dwork_over_40h Dmarried Ddivorced Dwidowed Dpopulation if male,noc robust first
outreg2 using Table5_Hetero.doc,nolabel replace coefastr se

ivreg2 Dhappy_4s (Dincome =IVDincome) DBirthOrder_ElderDummy Dbirth_weight Dearly_disease_d Dedu Dworking Dwork_over_40h Dmarried Ddivorced Dwidowed Dpopulation if ~male,noc robust first
outreg2 using Table5_Hetero.doc,nolabel append coefastr se

//no robust se to allow for hausman test (cross-specificaiton coeff comparison)
ivreg2 Dhappy_4s (Dincome =IVDincome) DBirthOrder_ElderDummy Dbirth_weight Dearly_disease_d Dedu Dworking Dwork_over_40h Dmarried Ddivorced Dwidowed Dpopulation if male,noc 
est store maleonly

ivreg2 Dhappy_4s (Dincome =IVDincome) DBirthOrder_ElderDummy Dbirth_weight Dearly_disease_d Dedu Dworking Dwork_over_40h Dmarried Ddivorced Dwidowed Dpopulation if ~male,noc 
est store femaleonly

hausman femaleonly maleonly
hausman femaleonly maleonly, force

//by age groups
ivreg2 Dhappy_4s (Dincome DInt_Inc_Age2635 DInt_Inc_Age3645 DInt_Inc_Age4655 DInt_Inc_Ageabove55=IVDincome IVDInt_Inc_Age2635 IVDInt_Inc_Age3645 IVDInt_Inc_Age4655 IVDInt_Inc_Ageabove55) DBirthOrder_ElderDummy Dbirth_weight Dearly_disease_d Dedu Dworking Dwork_over_40h Dmarried Ddivorced Dwidowed Dpopulation,noc robust
outreg2 using Table5_Hetero.doc,nolabel append coefastr se

//by  gender X age groups
ivreg2 Dhappy_4s (Dincome DInt_Inc_Age2635 DInt_Inc_Age3645 DInt_Inc_Age4655 DInt_Inc_Ageabove55=IVDincome IVDInt_Inc_Age2635 IVDInt_Inc_Age3645 IVDInt_Inc_Age4655 IVDInt_Inc_Ageabove55) DBirthOrder_ElderDummy Dbirth_weight Dearly_disease_d Dedu Dworking Dwork_over_40h Dmarried Ddivorced Dwidowed Dpopulation if male,noc robust
outreg2 using Table5_Hetero.doc,nolabel append coefastr se

ivreg2 Dhappy_4s (Dincome DInt_Inc_Age2635 DInt_Inc_Age3645 DInt_Inc_Age4655 DInt_Inc_Ageabove55=IVDincome IVDInt_Inc_Age2635 IVDInt_Inc_Age3645 IVDInt_Inc_Age4655 IVDInt_Inc_Ageabove55) DBirthOrder_ElderDummy Dbirth_weight Dearly_disease_d Dedu Dworking Dwork_over_40h Dmarried Ddivorced Dwidowed Dpopulation if ~male,noc robust
outreg2 using Table5_Hetero.doc,nolabel append coefastr se

clear
exit 
